package com.AAL.DAO;

import java.util.List;

import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IContractDetailDAO;
import com.AAL.Model.ContractDetail;
import com.AAL.Model.PurchaseContract;
import com.AAL.Utils.HibernateUtil;

public class ContractDetailDAO implements IContractDetailDAO {

	@Override
	public ContractDetail saveAndUpdateContractDetail(
			ContractDetail contractDetail) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(contractDetail);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return contractDetail;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContractDetail> getLstContractDetailsByContract(
			PurchaseContract purchaseContract) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ContractDetail> lstContractDetails = null;
		try {
			lstContractDetails = session
					.createQuery(
							"from ContractDetail where purchaseContract.purchaseContractID=:purchaseContractID ")
					.setParameter("purchaseContractID",
							purchaseContract.getPurchaseContractID()).list();
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return lstContractDetails;
	}

}
